const app = getApp()
import http from "../../utils/request.js";
const publicFun = require('../../utils/public');

const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        height: 0,
        sysUserInfo: {},
        avatarUrl: defaultAvatarUrl,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            height: wx.getSystemInfoSync().windowHeight
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.setData({
            sysUserInfo: app.globalData.sysUserInfo,
        })
    },

    //选择上传头像
    onChooseAvatar(e) {
        let suffix = e.detail.avatarUrl.substr(e.detail.avatarUrl.lastIndexOf('.') + 1)
        let params = {
            imgUrl: e.detail.avatarUrl,
            userId: app.globalData.userId,
            type: '个人头像',
            suffix: suffix
        }
        publicFun.uploadImg(params).then(res => {
            this.data.sysUserInfo.avatarUrl = res.url
            this.setData({
                sysUserInfo: this.data.sysUserInfo
            })
            console.log(this.data.sysUserInfo.avatarUrl);
        })
    },

    // 昵称输入变化
    changeNickName(e) {
        this.data.sysUserInfo.nickName = e.detail.value
        this.setData({
            sysUserInfo: this.data.sysUserInfo
        })
    },

    //签语输入变化
    changeSign(e) {
        this.data.sysUserInfo.signature = e.detail.value
        this.setData({
            sysUserInfo: this.data.sysUserInfo
        })
    },

    //修改用户信息
    toSave() {
        let data = {
            userId: app.globalData.userId,
            nickName: this.data.sysUserInfo.nickName,
            avatarUrl: this.data.sysUserInfo.avatarUrl,
            signature: this.data.sysUserInfo.signature
        }
        http.Request({
            url: 'fixUser',
            method: 'POST',
            data
        }).then(res => {
            publicFun.Toast(res.msg)
            app.getSysUser()
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})